package com.pmsystem.util;

import java.util.Date;
import java.util.HashMap;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pmsystem.pojo.CleanArea;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;

public class BeanUtil {

	//过滤map中的键值对
	public static Map<String, Object> mapToMapWithoutNull(Map<String, Object> map) {
		Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<String, Object> entry = iterator.next();
			Object value =  entry.getValue();
			if (!isValid(value)) {
				iterator.remove();
			}
		}
		return map;
	}
	
	public static <T> T parserToBean(Map<String, Object> map, Class<T> objClass) {
		T obj = null;
		try {
			obj = objClass.newInstance();
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();
				if (map.containsKey(key)) {
					Object value = map.get(key);
					Method setter = property.getWriteMethod();
					setter.invoke(obj, value);
				}
			}
		} catch (Exception e) {
			System.out.println("transMapToBean Error " + e);
		}
		return obj;
	}

	//无有意义值则返回false
	public static boolean isValid(Object... objs) {
		boolean isValid = true;
		if (null != objs) {
			for (int i = 0; i < objs.length; i++) {
				if(null == objs[i]  ) {
					isValid = false;
				}
				if(objs[i] instanceof String) {
					String key = (String)objs[i];
					if(key.trim().equals("")) {
						isValid = false;
					}
					if(key.trim().equals("-1")) {
						isValid = false;
					}
				} else if(objs[i] instanceof Integer) {
					Integer key = (Integer)objs[i];
					if(key.intValue()<=0) {
						isValid = false;
					}
				} else if(objs[i] instanceof Double) {
					Double key = (Double)objs[i];
					if(key.doubleValue()<=0) {
						isValid = false;
					}
				} else if(objs[i] instanceof Byte) {
					Byte key = (Byte)objs[i];
					if(key.byteValue() < 0) {
						isValid = false;
					}
				}else if (objs[i] instanceof Long) {
					Long key = (Long)objs[i];
					if(key.longValue() <= 0) {
						isValid = false;
					}
				}else if (objs[i] instanceof List<?>) {
					List<?> key = (List<?>)objs[i];
					if(key.size() <= 0) {
						isValid = false;
					}
				}else if (objs[i] instanceof JSONArray) {
					JSONArray key = (JSONArray)objs[i];
					if(key.size() <= 0) {
						isValid = false;
					}
				}else if (objs[i] instanceof JSONObject) {
					JSONObject key = (JSONObject)objs[i];
					if(key.size() <= 0) {
						isValid = false;
					}
				}
			}
		}else {
			isValid = false;
		}
		return isValid;
	}
	
	//测试例子
	public static void main(String[] args) {
//		Map<String, Object> map = new HashMap<>();
//		map.put("qwe", "12323213");
//		map.put("asd", null);
//		map.put("zxc", "	  ");
//		for (Map.Entry<String, Object> entry : map.entrySet()) {
//			System.out.println(entry.getKey() + ":" + entry.getValue());
//		}
//		System.out.println("------------------------");
//		map = mapToMapWithoutNull(map);
//		for (Map.Entry<String, Object> entry : map.entrySet()) {
//			System.out.println(entry.getKey() + ":" + entry.getValue());
//		}
//		System.out.println(System.getProperty("java.io.tmpdir"));
//		System.out.println( new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
//		Date date = new Date();
//		String today = date.toString();
//		System.out.println(today);
		System.out.println(BeanUtil.isValid("    "));
	}
}
